﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ExcelUtilities.Data
{
    /// <summary>
    /// 单元格的值
    /// </summary>
    public class CellValue
    {
        private object value;

        /// <summary>
        /// 用数据值初始化CellValue
        /// </summary>
        /// <param name="obj">初始值</param>
        public CellValue(object obj)
        {
            value = obj;
        }

        /// <summary>
        /// 值是否是空
        /// </summary>
        public bool IsNull
        {
            get
            {
                return value == null;
            }
        }
         
        #region 运算符重载

        public static implicit operator CellValue(int i)
        {
            return new CellValue(i);
        }
        public static implicit operator int(CellValue cv)
        {
            return cv.ToInt(0).Value;
        }
        public static implicit operator CellValue(float f)
        {
            return new CellValue(f);
        }
        public static implicit operator float(CellValue cv)
        {
            return cv.ToFloat(0F).Value;
        }
        public static implicit operator CellValue(DateTime dt)
        {
            return new CellValue(dt);
        }
        public static implicit operator DateTime(CellValue cv)
        {
            DateTime? dt = cv.ToDateTime();
            if (dt.HasValue)
                return dt.Value;
            else
                return DateTime.MinValue;
        }
        public static implicit operator CellValue(string s)
        {
            return new CellValue(s);
        }
        public static implicit operator string(CellValue cv)
        {
            return cv.value == null ? "" : cv.ToString();
        }

        public override string ToString()
        {
            return value == null ? "" : value.ToString();
        }

        #endregion

        #region 类型转换和判断函数

        /// <summary>
        /// 返回单元格的值是否是浮点型
        /// </summary>
        public bool IsFloat
        {
            get
            {
                if (value == null)
                    return false;
                else
                {
                    float f;
                    return float.TryParse(value.ToString(), out f);
                }
            }
        }
        /// <summary>
        /// 返回单元格的值是否是整型
        /// </summary>
        public bool IsInt
        {
            get
            {
                if (value == null)
                    return false;
                else
                {
                    int i;
                    return int.TryParse(value.ToString(), out i);
                }
            }
        } 
        /// <summary>
        /// 返回单元格的值是否是日期型
        /// </summary>
        public bool IsDateTime
        {
            get
            {
                if (value == null)
                    return false;
                else
                {
                    DateTime dt;
                    return DateTime.TryParse(value.ToString(), out dt);
                }
            }
        }

        /// <summary>
        /// 单元格值转成浮点型
        /// </summary>
        /// <param name="default">默认值</param>
        /// <returns></returns>
        public float? ToFloat(float? @default = null)
        {
            float result;
            if (value != null && float.TryParse(value.ToString(), out result))
                return result;
            else
            {
                return @default;
            }
        }
        /// <summary>
        /// 单元格值转成整型
        /// </summary>
        /// <param name="default">默认值</param>
        /// <returns></returns>
        public int? ToInt(int? @default = null)
        {
            int result;
            if (value != null && int.TryParse(value.ToString(), out result))
                return result;
            else
            {
                return @default;
            }
        }
        /// <summary>
        /// 单元格值转成日期型
        /// </summary>
        /// <param name="default">默认值</param>
        /// <returns></returns>
        public DateTime? ToDateTime(DateTime? @default = null)
        {
            DateTime result;
            string str = value == null ? "" : value.ToString();
            string dateTimeFormat = "1900-01-01 00:00:00";
            if (!string.IsNullOrWhiteSpace(str))
            {
                str = str + dateTimeFormat.Substring(str.Length, dateTimeFormat.Length - str.Length);
            }

            if (DateTime.TryParse(str, out result))
                return result;
            else
            {
                return @default;
            }
        }
          
        #endregion
    }
}